package com.dove.ohpm.ui;

import com.dove.ohpm.ui.custom.OperateType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import org.apache.maven.artifact.versioning.ComparableVersion;

import javax.swing.*;
import java.awt.*;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

class viewExt {
    public static String key;
    public static Boolean useCustom = false;
    public static String customHostUrl = "";

    public static JPanel getButtonPanel(String packageName, String latestVersion, String currentVersion, String packageId) {
        JPanel plButton = new JPanel();
        var size = new Dimension(78, 34);
        plButton.setLayout(new BoxLayout(plButton, BoxLayout.Y_AXIS));
        final var linkButton = new JButton();
        linkButton.addActionListener(e -> viewExt.openLink(packageName, packageId));
        linkButton.setSize(size);
        linkButton.setText("查看");
        plButton.add(linkButton);
        if (currentVersion == null || currentVersion.isEmpty()) {
            final var button = new JButton();
            button.addActionListener(e -> {
                try {
                    button.setEnabled(false);
                    clickButton(OperateType.Install, key, packageName, latestVersion);
                } catch (Exception ex) {
                    Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
                }
            });
            button.setSize(size);
            button.setText("安装");
            plButton.add(button);
        } else {
            final var btnDel = new JButton();
            btnDel.setSize(size);
            btnDel.setText("移除");
            btnDel.addActionListener(e -> {
                try {
                    btnDel.setEnabled(false);
                    clickButton(OperateType.Delete, key, packageName, "");
                } catch (Exception ex) {
                    Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
                }
            });
            plButton.add(btnDel);
            if (new ComparableVersion(currentVersion).compareTo(new ComparableVersion(latestVersion)) < 0) {
                var button = new JButton();
                button.setSize(size);
                button.setText("更新");
                button.addActionListener(e -> {
                    try {
                        button.setEnabled(false);
                        clickButton(OperateType.Update, key, packageName, latestVersion);
                    } catch (Exception ex) {
                        Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
                    }
                });
                plButton.add(button);
            }
        }
        return plButton;
    }

    public static void clickButton(OperateType type, String key, String packageName, String version) {
        KotlinListener.INSTANCE.onButtonClick(type, key, packageName, version);
    }

    public static void SetText(JLabel jLabel, String longString) {
        StringBuilder builder = new StringBuilder("<html>");
        char[] chars = longString.toCharArray();
        FontMetrics fontMetrics = jLabel.getFontMetrics(jLabel.getFont());
        int start = 0, len = 0;
        while (start + len < longString.length()) {
            if (builder.length() > 6) {
                builder.append("<br/>");
            }
            while (true) {
                len++;
                if (start + len > longString.length()) break;
                if (fontMetrics.charsWidth(chars, start, len) > jLabel.getWidth()) {
                    break;
                }
            }
            builder.append(chars, start, len - 1);
            start = start + len - 1;
            len = 0;
        }
        builder.append(chars, start, longString.length() - start);
        builder.append("</html>");
        jLabel.setText(builder.toString());
    }

    public static void openLink(String packageName, String packageId) {
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            try {
                packageName = URLEncoder.encode(packageName, StandardCharsets.UTF_8);
                var uri = useCustom ?
                        (customHostUrl + "/#/cn/detail/" + packageName + "?id=" + packageId) :
                        ("https://ohpm.openharmony.cn/#/cn/detail/" + packageName);
                desktop.browse(new URI(uri));
            } catch (Exception ex) {
                Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
            }
        }
    }
}